package com.noodles.mall.controller;

import com.noodles.mall.entity.Cart;
import com.noodles.mall.entity.Order;
import com.noodles.mall.entity.User;
import com.noodles.mall.service.OrderItemService;
import com.noodles.mall.service.OrderService;
import com.noodles.mall.service.UserService;
import com.noodles.mall.vo.PageVo;
import com.noodles.mall.web.JsonResult;
import com.noodles.mall.web.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/** 订单 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private UserService userService;

    /** 增加 **/
    @RequestMapping("/insert")
    public JsonResult insert(@RequestBody List<Cart> cartList) {
        System.out.println("OrderController: insertOrder");
        System.out.println("cartList: "+cartList);
        User user=userService.selectOne(cartList.get(0).getUserId());
        Order order = orderService.insert(user,cartList);
        return orderItemService.insertBath(order,cartList)
                ? JsonResult.success(order) : JsonResult.error(State.ERR_SQL);
    }

    /** 删除:  **/
    @RequestMapping("/delete")
    public JsonResult delete(@RequestBody List<Integer> idList) {
        System.out.println("OrderController: delete");
        System.out.println("idList: "+idList);
        return orderService.delete(idList)
                ? JsonResult.success() : JsonResult.error(State.ERR_SQL);
    }

    /** 修改订单 **/
    @RequestMapping("/update")
    public JsonResult update(@RequestBody Order order) {
        System.out.println("OrderController: updateOrder");
        System.out.println("order: "+order);
        //没写
        boolean f=true;
        return f ? JsonResult.success() : JsonResult.error(State.ERR_SQL);
    }

    /** 查询单个: id **/
    @RequestMapping("/selectById/{id}")
    public JsonResult selectById(@PathVariable(value = "id") Integer id) {
        System.out.println("OrderController: selectOrderDetails");
        System.out.println("id: "+id);
        Order order = orderService.selectOne(id);
        return JsonResult.success(order);
    }

    /** 查询: 订单数量 **/
    @RequestMapping("/selectCount")
    public JsonResult selectCount() {
        System.out.println("OrderController: selectCount");
        int num = orderService.selectCount();
        return JsonResult.success(num);
    }

    /** 分页查询: 无 **/
    @RequestMapping("/selectPage/{current}/{size}")
    public JsonResult selectPageByWord(@PathVariable(value = "current") Long current,
                                       @PathVariable(value = "size") Long size) {
        System.out.println("OrderController: selectOrderByUserId");
        System.out.println("current: "+current);
        System.out.println("size: "+size);
        PageVo pageVo = orderService.selectPage(current,size);
        return JsonResult.success(pageVo);
    }

    /** 分页查询: userId **/
    @RequestMapping("/selectPage/{current}/{size}/{userId}")
    public JsonResult selectPageByWord(@PathVariable(value = "current") Long current,
                                       @PathVariable(value = "size") Long size,
                                       @PathVariable(value = "userId") Integer userId) {
        System.out.println("OrderController: selectPageByUserId");
        System.out.println("current: "+current);
        System.out.println("size: "+size);
        System.out.println("userId: "+userId);
        PageVo pageVo = orderService.selectPage(current,size,userId);
        return JsonResult.success(pageVo);
    }

    /** 分页查询: word **/
    @RequestMapping("/selectPageByWord/{current}/{size}/{word}")
    public JsonResult selectPageByWord(@PathVariable(value = "current") Long current,
                                       @PathVariable(value = "size") Long size,
                                       @PathVariable(value = "word") String word) {
        System.out.println("OrderController: selectOrderByUserId");
        System.out.println("current: "+current);
        System.out.println("size: "+size);
        System.out.println("word: "+word);
        PageVo pageVo = orderService.selectPage(current,size,word);
        return JsonResult.success(pageVo);
    }

}
